package net.funwoo.pandago.ui;

import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import net.funwoo.pandago.AppApplication;
import net.funwoo.pandago.AppConfig;
import net.funwoo.pandago.R;
import net.funwoo.pandago.util.SystemBarTintManager;

import de.greenrobot.event.EventBus;

/**
 * Created by Administrator on 2015/5/6.
 */
public abstract class BaseActivity extends AppCompatActivity {

    protected Context mContext;
    private TextView mTitleView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setupSystemBar();
        super.onCreate(savedInstanceState);

        mContext = this;
    }

    protected void setupSystemBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
    }

    protected void setContentView(int layoutResID, boolean useToolbar) {
        if (useToolbar) {
            super.setContentView(R.layout.activity_toolbar);
            View view = getLayoutInflater().inflate(layoutResID, null, false);
            ((LinearLayout) findViewById(R.id.toolbar_container)).addView(view);
        } else {
            super.setContentView(layoutResID);
        }

        setupToolbar(getResources().getColor(R.color.blue));

        initView();
    }

    protected void setupToolbar(int themeColor) {
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        if (toolbar != null) {
            toolbar.setBackgroundColor(themeColor);
            setSupportActionBar(toolbar);
            ActionBar bar = getSupportActionBar();
            if (bar != null) {
                if (bar.getTitle() != null) {
                    String title = bar.getTitle().toString();
                    View view = findViewById(R.id.toolbar_title);
                    if (view != null) {
                        mTitleView = (TextView) view;
                        mTitleView.setText(title);
                    }
                    bar.setTitle(null);
                }

                bar.setHomeAsUpIndicator(R.drawable.ic_return);
                bar.setDisplayHomeAsUpEnabled(true);
            }
        }

        // set compat status color in kitkat or later devices
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            SystemBarTintManager tintManager = new SystemBarTintManager(this);
            tintManager.setStatusBarTintEnabled(AppConfig.TRANSLUCENT_BAR_ENABLED);
            tintManager.setTintColor(themeColor);
        }
    }

    @Override
    public void setTitle(CharSequence title) {
        mTitleView.setText(title);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    protected AppApplication getAppInstance() {
        return AppApplication.getInstance();
    }

    protected void postEvent(Object event) {
        EventBus.getDefault().post(event);
    }

    protected void postStickyEvent(Object event) {
        EventBus.getDefault().postSticky(event);
    }

    protected abstract void initView();
}
